import 'package:FiduMart/app_routes.dart';
import 'package:FiduMart/constants/constants.dart';
import 'package:FiduMart/core/utils/common_util.dart';
import 'package:FiduMart/core/utils/image_constant.dart';
import 'package:FiduMart/core/utils/logger.dart';
import 'package:FiduMart/core/utils/pref_utils.dart';
import 'package:FiduMart/core/utils/stringUtil.dart';
import 'package:FiduMart/localization/app_export.dart';
import 'package:FiduMart/widget/app_bar/custom_app_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:FiduMart/screen/webview/web_controller.dart';
import 'package:FiduMart/widget/title_bar.dart';
import 'package:flutter/widgets.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:io';

class WebviewScreen extends GetWidget<WebController> {
  var title = '';
  final pref = Get.find<PrefUtils>();
  var html = Get.parameters['html'] ?? '';
  var timeStr = Get.parameters['time'] ?? 'dfdf';

  @override
  Widget build(BuildContext context) {
    if (title.isEmpty && Get.parameters['title'] != null) {
      title = Get.parameters['title']!;
    }
    return bodyColum(context);
  }

  Widget bodyColum(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        Column(
          children: [
            SizedBox(
              height: 30,
            ),
            buildTitle(context)
          ],
        ),
        Obx(() => controller.loading == true
            ? Expanded(
                child: Center(
                  child: CircularProgressIndicator.adaptive(),
                ),
              )
            : html.isEmpty
                ? Expanded(child: WebViewWidget(controller: controller.wvController))
                : Expanded(
                  child: Container(
                      margin: EdgeInsets.all(10),
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage(
                            "assets/images/bg_webview.png",
                          ),
                          fit: BoxFit.fill,
                        ),
                        borderRadius: BorderRadius.circular(10),
                        border:
                        Border.all(color: Color(0xffd5d5d5), width: 1),
                      ),
                      child: Column(
                        children: [
                          Row(
                            children: [
                              Image.asset("assets/images/icon_launcher.png",
                                  width: 46),
                              SizedBox(width: 10),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text("FiduMart",style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold,color: Colors.black),),
                                  Text(timeStr,style: TextStyle(color: ColorConstant.text_grey,fontSize: 14))
                                ],
                              )
                            ],
                          ),
                          SizedBox(height: 16,),
                          Text(
                            title,
                            style: TextStyle(color: Colors.black,fontSize: 21,fontWeight: FontWeight.bold),
                          ),
                          SizedBox(height: 16,),
                          Expanded(
                            child: WebViewWidget(
                                controller: controller.wvController),
                          ),
                        ],
                      )),
                )),
      ],
    ));
  }

  Future<void> onBackButtonPressed() async {
    if (await controller.wvController.canGoBack()) {
      // 如果WebView可以返回上一级，则返回上一级
      controller.wvController.goBack();
    } else {
      // 如果WebView不能返回上一级（即返回栈为空），则关闭页面
      if (Platform.isIOS) controller.wvController.clearCache();
      Get.back();
    }
  }

  Row buildTitle(BuildContext context) {
    return Row(
      children: [
        SizedBox(
          width: 10,
        ),
        IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 24,
            color: Colors.black,
          ),
          onPressed: () {
            onBackButtonPressed();
          },
        ),
        Expanded(
            child: Center(
                child: Text(
                    StringUtil.isEmpty(html) ? title: "info_detail".tr,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                        color: Colors.black)))),
        SizedBox(
          width: 50,
        ),
      ],
    );
  }

}
